package view.Arm
{
	import data.Globals;
	
	import flash.events.Event;
	import flash.geom.Point;
	import flash.utils.getTimer;
	
	import utils.GameUtils;
	import utils.HitTest;

	/**
	 *
	 *@author CrazyChen 
	 *2013-4-15
	 */	
	public class BasePlane extends BaseTank
	{
		protected var _b_num:int;
		
		protected var _speed:int; 
		
		protected var _x_speed:int;
		protected var _y_speed:int;
		protected var _killSelfRat:int;
		
		private var goHome:Boolean;
		
		private var isOpenKill:Boolean;
		
		private var isCheckRandom:Boolean;
		
		public function BasePlane()
		{
			super();
		}
		
		override protected function loop(event:Event):void{
			if(isOpenKill){
				chechTargetIsDie();
				if(shoutTarget){
					var pp:Point=GameUtils.getSpeed(new Point(this.x,this.y),new Point(shoutTarget.x,shoutTarget.y),speed);
					x_speed = pp.x;
					y_speed = pp.y;
					move();
					checkHitMonster();
				}else{
					goHome = true;
					isOpenKill  =false;
				}
			}
			if(goHome){
				if(isCheckRandom==false){
					if(killSelfAttact())return;
				}
				this.x += speed;
				if(this.x>700){
					die();
					return;
				}
				if(this.rotation<=90+speed && this.rotation>= 90-speed){
					this.rotation=90;
					return;
				}else{
					if(this.rotation>-90&&this.rotation<=90){
						this.rotation+=speed;
					}else{
						this.rotation-=speed;
					}
				
				}
			
			}
			
			if(isCloseLoop){
				return;
			}
			if(shoutTarget){
				var distance:int = GameUtils.getTwoPointDistance(new Point(shoutTarget.x,shoutTarget.y),new Point(this.x,this.y));
				if(distance > shoutRadius){
					findTarget();
				}else{
					chechTargetIsDie();
				}
			}
			if(!shoutTarget)findTarget();
			if(shoutTarget){
				var p:Point = GameUtils.getSpeed(new Point(this.x,this.y),new Point(shoutTarget.x,shoutTarget.y),speed);
				var dis:int = GameUtils.getTwoPointDistance(new Point(this.x,this.y),new Point(shoutTarget.x,shoutTarget.y));
				var gap:int = Math.abs(dis-200);
				x_speed = p.x;
				y_speed = p.y;
				if(gap<speed){
					p = GameUtils.getSpeed(new Point(this.x,this.y),new Point(shoutTarget.x,shoutTarget.y),gap);
					x_speed = p.x;
					y_speed = p.y;
					if(dis-200>0){
						move();
					}else{
						this.x -= x_speed;
						this.y -= y_speed;
					}
				}else if(dis>200+speed){
					move();
				}
				turnDirection();
			}
		}
		
		private function checkHitMonster():void{
	          if(HitTest.complexHitTestObject(this,shoutTarget)){
				  shoutTarget['blood'] = -1;
				  blood = -1;
			  }
		}
		
		protected function move():void{
			this.x+=x_speed;
			this.y+=y_speed;
		}
		
		override protected function shout():void{
			var isShoot:Boolean;
			if(preTime){
				var curUse:Number = getTimer() - preTime;
				if(curUse > shoutGap){
					preTime = getTimer();
					isShoot = true;
				}
			}else{
				preTime = getTimer();
				isShoot = true;
			}
			if(isShoot){
				Globals.gameView.shoutBullet(bulletType,new Point(this.x,this.y),new Point(shoutTarget.x,shoutTarget.y),null,8,-5);
				Globals.gameView.shoutBullet(bulletType,new Point(this.x,this.y),new Point(shoutTarget.x,shoutTarget.y),null,-8,-5);
				b_num -= 2;
				if(b_num<=0){
					goHome = true;
					isCloseLoop = true;
				}
				this.x -= x_speed;
				this.y -= y_speed;
			}
		}
		
		/**
		 * 启动自杀式攻击
		 */
		private function killSelfAttact():Boolean{
			if(Math.random()*100>killSelfRat){
				isOpenKill = true;
				goHome = false;
			}
			isCheckRandom = true;
			return isOpenKill;
		}
		
		override public function die():void{
			super.die();
			resetData();
		}
		
		private function resetData():void{
			goHome = false;
			isOpenKill = false;
			isCheckRandom = false;
		}

		public function get b_num():int
		{
			return _b_num;
		}

		public function set b_num(value:int):void
		{
			_b_num = value;
		}

		/**
		 *飞行速度 
		 */
		public function get speed():int
		{
			return _speed;
		}

		/**
		 * @private
		 */
		public function set speed(value:int):void
		{
			_speed = value;
		}

		public function get y_speed():int
		{
			return _y_speed;
		}

		public function set y_speed(value:int):void
		{
			_y_speed = value;
		}

		public function get x_speed():int
		{
			return _x_speed;
		}

		public function set x_speed(value:int):void
		{
			_x_speed = value;
		}

		public function get killSelfRat():int
		{
			return _killSelfRat;
		}

		public function set killSelfRat(value:int):void
		{
			_killSelfRat = value;
		}


	}
}